<template>
	<view style="margin: 0 26rpx;">
		<view class="add flex flex-ai-c flex-jc-c" v-if="!addressList.length"
			@click="skip('/pages/equipment/replenishment/address?state=1')">
			<i-icon icon="lujing" />
			<view>添加收货的地址</view>
		</view>
		<view v-else class="add flex flex-ai-c flex-jc-sb"
			@click="skip('/pages/equipment/replenishment/address?state=1')">
			<view style="width: 560rpx;">
				<view class="flex flex-ai-c">
					<view class="moren" v-if="currentAddr.is_default">默认</view>
					<view class="ellipsis">{{currentAddr.citys}}</view>
				</view>
				<view class="ellipsis font-32 font-bold" style="margin: 10rpx 0;">{{currentAddr.detail}}</view>
				<view>{{currentAddr.name}}
					{{currentAddr.phone ? currentAddr.phone.replace(/^(\d{3})\d{4}(\d{4})$/, "$1****$2"): ''}}
				</view>
			</view>
			<u-icon name="arrow-right" />
		</view>
		<view class="list">
			<view class="flex flex-ai-c flex-jc-sb list-c">
				<view>供应商</view>
				<view>{{info.supplier}}</view>
			</view>
			<view class="flex flex-ai-c flex-jc-sb list-c">
				<view>订单号</view>
				<view>{{info.order_no}}</view>
			</view>
			<view class="flex flex-ai-c flex-jc-sb list-c">
				<view>采购商品</view>
				<view>共{{info.total_num}}件</view>
			</view>
			<view class="flex flex-ai-c flex-jc-sb list-c">
				<view>订单金额</view>
				<view>￥{{info.total_amount}}</view>
			</view>
			<view class="flex flex-ai-c flex-jc-sb list-c">
				<view>运费</view>
				<view>￥{{info.freight}}</view>
			</view>
		</view>
		<view class="paytype">
			<view style="color: #666666;margin-bottom: 10rpx;" class="font-26">支付方式</view>
			<view v-for="(item,i) in list" :key="i">
				<view class="flex flex-jc-sb flex-ai-c" style="margin-bottom: 20rpx;">
					<view class="flex flex-ai-c">
						<view style="margin-right: 20rpx;margin-top: 12rpx;"><i-icon :icon="item.icon" size="30" />
						</view>
						<view>
							{{item.name}}
							{{item.balance?`(￥${balance})`: ''}}
						</view>
					</view>
					<radio color="rgb(252,186,13)" @click="radioChange(i)" :value="item.value" :checked="index == i">
					</radio>
				</view>
			</view>
			<rich-text user-select :nodes="bankInfo" :selectable="true" v-show="bankInfoState"></rich-text>
		</view>

		<view class="sum flex flex-ai-c flex-jc-sb">
			<view class="sum-price flex flex-ai-c">
				<view>合计：</view>
				<view>￥{{info.total_amount}}</view>
			</view>
			<view class="sum-btn" @click="submit">
				提交订单
			</view>
		</view>
	</view>
</template>

<script>
	import {
		interval
	} from '@/utils/util.js';
	import common from '@/api/common.js';
	import product from '@/api/product.js';
	import user from '@/api/user.js';
	export default {
		data() {
			return {
				index: '1',
				list: [{
						icon: 'yue',
						name: '余额支付',
						value: '1',
						balance: true
					},
					{
						icon: 'weixin',
						name: '微信支付',
						value: '2'
					},
					{
						icon: 'huikuan',
						name: '对公汇款',
						value: '6'
					}
				],
				addressList: [],
				bankInfoState: false,
				bankInfo: '',
				currentAddr: {},
				balance: '0.00',
				info: {},
				state: 0,
				order_no: null,
				id: null
			}
		},
		computed: {
			sumPrice() {
				return interval(13173, 2)
			},
			/* info() {
				return this.$store.state.supplementGoods
			} */
		},
		onShow() {
			let pages = getCurrentPages();
			let currPage = pages[pages.length - 1]; // 当前页的实例
			let id = currPage.$vm.value
			if (this.id == id) return
			if (id == 'del') {
				this.getAddressList((res) => {
					if (res && res.length) {
						this.currentAddr = res[0]
					} else {
						this.currentAddr = {}
					}
					this.id = id
				})
			} else if (id) {
				this.getAddressList((res) => {
					this.currentAddr = res.find(v => v.id == id)
					this.id = id
				})
			}
		},
		onLoad(op) {
			if (!op.order_no) return
			this.order_no = op.order_no
			if (op.state) {
				this.state = 1
			}
			this.orderDetail(op.order_no)
			this.getOpenBankInfo()
			this.getAddressList(() => {})
			this.getUserInfo()
		},
		methods: {
			async orderDetail(order_no) {
				const res = await product.orderDetail(order_no)
				if (res.code === 200) {
					this.currentAddr = res.data.address_info
					this.info = res.data
				}
			},
			async getUserInfo() {
				const res = await user.getUserInfo()
				this.balance = res.data.cost_amount || '0.00'
			},
			// 地址
			async getAddressList(callback) {
				const res = await user.getAddressList()
				if (!res.data.length) return
				this.addressList = res.data
				callback(res.data)
				// this.currentAddr = res.data[0]
			},
			//开户信息
			async getOpenBankInfo() {
				const res = await common.getOpenBankInfo()
				if (res.code === 200) this.bankInfo = res.data.opening_bank_info
			},
			skip(url) {
				uni.navigateTo({
					url: url
				})
			},
			///支付切换
			radioChange(e) {
				this.index = e
				if (e === 2) {
					this.bankInfoState = true
				} else {
					this.bankInfoState = false
				}
			},
			showMsg(msg) {
				uni.showToast({
					title: msg,
					icon: 'none'
				})
			},
			back() {
				setTimeout(() => {
					if (this.state) {
						uni.navigateTo({
							url: '/pages/equipment/replenishment/order'
						})
					} else {
						uni.navigateBack()
					}

				}, 1000)
			},
			switchPay(type, payInfo) {
				let _this = this
				switch (type) {
					case "1": //余额支付
						_this.showMsg('支付成功');
						_this.back()
						break
					case "2": //微信支付
						uni.requestPayment({
							timeStamp: payInfo.timeStamp,
							nonceStr: payInfo.nonceStr,
							package: payInfo.package,
							signType: payInfo.signType,
							paySign: payInfo.paySign,
							success(res2) {
								_this.showMsg('支付成功');
								_this.back()
							},
							fail(err2) {
								if (err2.errMsg === 'requestPayment:fail cancel') {
									_this.showMsg('您已取消支付');
								} else {
									_this.showMsg('支付失败');
								}
							}
						})

						break
					case "6": //对公
						_this.showMsg('支付成功');
						_this.back()
						break
				}
			},
			async orderPay(order) {
				const res = await product.orderPay(order)
				if (res.code == 200) {
					let type = this.list[this.index].value
					this.switchPay(type, res.data.payRes)
				}
			},
			submit() {
				let type = this.list[this.index].value
				// this.$store.state.supplementGoods
				// this.currentAddr
				let order = {
					order_no: this.info.order_no,
					address_id: this.currentAddr.id,
					pay_way: this.list[this.index].value,
					total_amount: this.info.total_amount
				}
				this.orderPay(order)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.add {
		height: 208rpx;
		margin: 0 30rpx;

		.moren {
			width: 60rpx;
			height: 26rpx;
			background: #CB712B;
			border-radius: 5rpx;
			font-size: 20rpx;
			color: #fff;
			text-align: center;
			line-height: 26rpx;
			margin-right: 10rpx;
		}
	}

	.list {
		height: 341rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 0rpx 16rpx 3rpx rgba(238, 239, 240, 0.5);
		border-radius: 5rpx;

		.list-c {
			margin: 0 30rpx;
			line-height: 68rpx;
			font-size: 26rpx;

			view:last-child {
				font-weight: 600;
				font-size: 28rpx;
			}
		}
	}

	.paytype {
		margin: 60rpx 30rpx;
	}

	.sum {
		width: 100%;
		position: fixed;
		bottom: 0;
		right: 0;
		// padding: 0 26rpx;
		height: 160rpx;
		box-shadow: 2rpx 0rpx 4rpx 0rpx rgba(0, 0, 0, 0.5);

		.sum-price {
			margin-left: 26rpx;

			view:last-child {
				font-size: 60rpx;
				color: #333333;
			}

			view:first-child {
				font-size: 26rpx;
				color: #666666;
			}
		}

		.sum-btn {
			width: 224rpx;
			height: 74rpx;
			background: #FCB527;
			border-radius: 37rpx;
			color: #fff;
			text-align: center;
			line-height: 74rpx;
			font-size: 26rpx;
			margin-right: 26rpx;
		}
	}

	.ellipsis {
		display: -webkit-box;
		overflow: hidden;
		text-overflow: ellipsis;
		-webkit-line-clamp: 1;
		-webkit-box-orient: vertical;
	}
</style>